home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH05 / PGM5_4.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-10-09  |  2.3 KB  |  124 lines

  1. ; Multidimensional Array declaration and access
  2. ;
  3. ; Randall Hyde
  4.  
  5.  
  6.         .386            ;Need these two statements to use
  7.         option    segment:use16    ; 80386 register set.
  8.  
  9.  
  10. dseg        segment    para public 'data'
  11.  
  12.  
  13. ; Indices we will use for the arrays.
  14.  
  15. J        word    1
  16. K        word    2
  17. L        word    3
  18.  
  19. ; Some two-dimensional arrays.
  20. ; Note how this code uses the "dup" operator to suggest the size
  21. ; of each dimension.
  22.  
  23. B2Ary        byte    3 dup (4 dup (?))
  24. W2Ary        word    4 dup (3 dup (?))
  25. D2Ary        dword    2 dup (6 dup (?))
  26.  
  27.  
  28.  
  29. ; 2D arrays with initialization.
  30. ; Note the use of data layout to suggest the sizes of each array.
  31.  
  32. B2Ary2        byte    0, 1, 2, 3
  33.         byte    4, 5, 6, 7
  34.         byte    8, 9, 10, 11
  35.  
  36. W2Ary2        word    0,  1,  2
  37.         word    3,  4,  5
  38.         word    6,  7,  8
  39.         word    9, 10, 11
  40.  
  41. D2Ary2        dword    0,  1,  2,  3,  4,  5
  42.         dword    6,  7,  8,  9, 10, 11
  43.  
  44.  
  45. ; A sample three dimensional array.
  46.  
  47. W3Ary        word    2 dup (3 dup (4 dup (?)))
  48.  
  49. dseg        ends
  50.  
  51.  
  52.  
  53. cseg        segment    para public 'code'
  54.         assume    cs:cseg, ds:dseg
  55.  
  56. Main        proc
  57.         mov    ax, dseg    ;These statements are provided by
  58.         mov    ds, ax        ; shell.asm to initialize the
  59.         mov    es, ax        ; segment register.
  60.  
  61.  
  62. ; AL := B2Ary2[j,k]
  63.  
  64.         mov    bx, J        ;index := (j*4+k)
  65.         add    bx, bx        ;j*2
  66.         add    bx, bx        ;j*4
  67.         add    bx, K        ;j*4+k
  68.         mov    al, B2Ary2[bx]
  69.  
  70.  
  71. ; AX := W2Ary2[j,k]
  72.  
  73.         mov    ax, J        ;index := (j*3 + k)*2
  74.         mov    bx, 3
  75.         mul    bx        ;(j*3)-- This destroys DX!
  76.         add    ax, k        ;(j*3+k)
  77.         add    ax, ax        ;(j*3+k)*2
  78.         mov    bx, ax
  79.         mov    ax, W2Ary2[bx]
  80.  
  81.  
  82. ; EAX := D2Ary[i,j]
  83.  
  84.         mov    ax, J        ;index := (j*6 + k)*4
  85.         mov    bx, 6
  86.         mul    bx        ;DX:AX := j*6, ignore overflow in DX.
  87.         add    ax, k        ;j*6 + k
  88.         add    ax, ax        ;(j*6 + k)*2
  89.         add    ax, ax        ;(j*6 + k)*4
  90.         mov    bx, ax
  91.         mov    eax, D2Ary[bx]
  92.  
  93.  
  94. ; Sample access of a three dimensional array.
  95. ;
  96. ; AX := W3Ary[J,K,L]
  97.  
  98.         mov    ax, J        ;index := ((j*3 + k)*4 + l)*2
  99.         mov    bx, 3
  100.         mul    bx        ;j*3
  101.         add    ax, K        ;j*3 + k
  102.         add    ax, ax        ;(j*3 + k)*2
  103.         add    ax, ax        ;(j*3 + k)*4
  104.         add    ax, l        ;(j*3 + k)*4 + l
  105.         add    ax, ax        ;((j*3 + k)*4 + l)*2
  106.         mov    bx, ax
  107.         mov    ax, W3Ary[bx]
  108.  
  109.  
  110. Quit:        mov    ah, 4ch        ;Magic number for DOS
  111.         int    21h        ; to tell this program to quit.
  112. Main        endp
  113.  
  114. cseg        ends
  115.  
  116. sseg        segment    para stack 'stack'
  117. stk        byte    1024 dup ("stack   ")
  118. sseg        ends
  119.  
  120. zzzzzzseg    segment    para public 'zzzzzz'
  121. LastBytes    byte    16 dup (?)
  122. zzzzzzseg    ends
  123.         end    Main
  124.